package com.apkcore.core.mvi

import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow

// 一个例子，所以全private
private sealed class TestIntent : IUiIntent {
    data class GetListData(var code: Int) : TestIntent()
}

private sealed class TestUiState : IUIState {
//    data object Init : TestUiState()
    data class Success(val data: String?) : TestUiState()
    data class Fail(val msg: Throwable?) : TestUiState()
}

private class TestModel : BaseViewModel<TestUiState, TestIntent>() {
//    override fun initUIState(): TestUiState {
//        return TestUiState.Init
//    }

    override suspend fun handleIntent(intent: TestIntent) {
        when (intent) {
            is TestIntent.GetListData -> {
                getListData(intent.code)
            }
        }
    }

    private suspend fun getListData(code: Int) {
        flow {
            emit("123 $code")
        }.catch {
            sendUiState (TestUiState.Fail(it) )
        }.collect {
            sendUiState ( TestUiState.Success(it) )
        }
    }
}